package com.aidong.couponsRule.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.couponsRule.biz.TrnCouponsRuleBiz;
import com.aidong.couponsRule.entity.TrnCouponsRule;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnCouponsRuleAction implements ModelDriven<TrnCouponsRule> {

	private Map<String, Object> map; // 返回的json数据
	private TrnCouponsRule trnCouponsRule;// 代金券实体类
	private TrnCouponsRuleBiz trnCouponsRuleBiz;

	@Override
	public TrnCouponsRule getModel() {
		if (trnCouponsRule == null) {
			trnCouponsRule = new TrnCouponsRule();
		}
		return trnCouponsRule;
	}

	@Autowired
	public void setTrnCouponsRuleBiz(TrnCouponsRuleBiz trnCouponsRuleBiz) {
		this.trnCouponsRuleBiz = trnCouponsRuleBiz;
	}

	/**
	 * 返回的json数据
	 * 
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	/**
	 * 获取优惠券
	 * 
	 * @return
	 */
	@Action(value = "getCouponsRule", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getCouponsRule() {
		map = new HashMap<String, Object>();
		try {
			Map<String, Object> prMap = new HashMap<String, Object>();
			Map<String, Object> resultMap = trnCouponsRuleBiz.getCouponsRule(prMap);
			if (resultMap != null && Integer.parseInt(resultMap.get("num").toString()) == 1) {
				map.put("code", 1);
				map.put("beginTime", resultMap.get("beginTime"));
				map.put("endTime", resultMap.get("endTime"));
				map.put("price", resultMap.get("price"));
				map.put("tickNum", resultMap.get("tickNum"));
			} else {
				map.put("code", 0);
				map.put("msg", "暂无优惠券可用");
			}
			return "succ";
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 是否有优惠券
	 * 
	 * @return
	 */
	@Action(value = "getCouponsCou", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getCouponsCou() {
		map = new HashMap<String, Object>();
		try {
			Map<String, Object> prMap = new HashMap<String, Object>();
			int cou = trnCouponsRuleBiz.getCouponsCou(prMap);
			map.put("code", 0);
			map.put("cou", cou);
			return "succ";
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
